import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/dialog/dialog_route.dart';

class RouterObserver extends NavigatorObserver {
  final List<OnRouterListener> _rList = [];
  static final RouterObserver _instance = RouterObserver();

  static RouterObserver getInstance() {
    return _instance;
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    // showLog("didPop=====================");
    super.didPop(route, previousRoute);
    final currentRoute = _RouteData.ofRoute(route);
    final previous = _RouteData.ofRoute(previousRoute);
    if (currentRoute.isBottomSheet || currentRoute.isDialog) {
      // Get.log("CLOSE ${currentRoute.name}");
    } else if (currentRoute.isGetPageRoute) {
      // Get.log("CLOSE TO ROUTE ${currentRoute.name}");
      // showLog("close=======================${currentRoute.name.toString()} + ${previous.name} + ${Get.currentRoute}");
      if (_rList.isNotEmpty && currentRoute.name != null) {
        //分发事件
        for (var element in _rList) {
          element.didPop(
              currentRoute.name.toString(), previous.name.toString());
        }
      }
    }
  }

  @override
  void didPush(Route route, Route? previousRoute) {
    // showLog("didPush=====================");
    super.didPush(route, previousRoute);
    final newRoute = _RouteData.ofRoute(route);
    if (newRoute.isBottomSheet || newRoute.isDialog) {
      // Get.log("OPEN ${newRoute.name}");
    } else if (newRoute.isGetPageRoute) {
      // Get.log("GOING TO ROUTE ${newRoute.name}");
    }
  }

  @override
  void didRemove(Route route, Route? previousRoute) {
    // showLog("didRemove=====================");
    super.didRemove(route, previousRoute);
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    // showLog("didReplace=====================");
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
  }

  ///注册路由监听回调
  void registerListener(OnRouterListener onListener) {
    _rList.add(onListener);
  }

  ///解绑路由监听
  void unregisterListener(OnRouterListener onListener) {
    _rList.remove(onListener);
  }
}

/// This is basically a util for rules about 'what a route is'
class _RouteData {
  final bool isGetPageRoute;

  //final bool isSnackbar;
  final bool isBottomSheet;
  final bool isDialog;
  final String? name;

  _RouteData({
    required this.name,
    required this.isGetPageRoute,
    // required this.isSnackbar,
    required this.isBottomSheet,
    required this.isDialog,
  });

  factory _RouteData.ofRoute(Route? route) {
    return _RouteData(
      name: _extractRouteName(route),
      isGetPageRoute: route is GetPageRoute,
      // isSnackbar: route is SnackRoute,
      isDialog: route is GetDialogRoute,
      isBottomSheet: route is GetModalBottomSheetRoute,
    );
  }
}

/// Extracts the name of a route based on it's instance type
/// or null if not possible.
String? _extractRouteName(Route? route) {
  if (route?.settings.name != null) {
    return route!.settings.name;
  }

  if (route is GetPageRoute) {
    return route.routeName;
  }

  if (route is GetDialogRoute) {
    return 'DIALOG ${route.hashCode}';
  }

  if (route is GetModalBottomSheetRoute) {
    return 'BOTTOMSHEET ${route.hashCode}';
  }

  return null;
}

///路由监听回调方法
abstract class OnRouterListener {
  /// route 要销毁的页面名称
  /// previous 返回的页面名称
  void didPop(String route, String previous);
// void onResume(Routing routing);
// void onPause(Routing routing);
}
